#!/usr/bin/env bash
#
# compareRT version 1.0, 2014-01-24
# compareRT version 1.1, 2015-02-24
# Processes all images in the current dir with two different
# RawTherapee versions and tests for differences.
#
# Run this script from the dir with the images you want to test.
# Make sure the PP3 you set below is in the same dir.
# Don't worry about non-image files.
# Each dir specified in rtDirs and the outDir must end with /
#
# CC-BY-SA 3.0 DrSlony

unset rtDirs outDir pp3 imgs v i c

#--- Edit these as needed.
rtDirs=("$HOME/rt_default_release/" "$HOME/rt_default_release_patched/")
pp3="foo.pp3"
outDir="/tmp/compareRT/"
#---

while IFS=$'\n' read f; do
    imgs+=("$f")
done < <(find . -maxdepth 1 -type f -not -iname "*.txt" -not -iname "*.pp3" | sed "s_./__" | sort)

abort () {
    printf "%s\n" "" "Aborted"
    exit 1
}
trap 'abort' HUP INT QUIT ABRT TERM

mkdir -p "$outDir" || exit 1

i=0
for rtDir in "${rtDirs[@]}"; do
    c=1
    pp3name=${pp3%.*}
    pp3name=${pp3name#*/}
    v+=("$(grep "Commit:.*" "${rtDir}/AboutThisBuild.txt" | sed "s/Commit: //")")
    printf "%s\n" "Developing images using RawTherapee commit ${v[$i]} - ${rtDir}"
    for img in "${imgs[@]}"; do
        printf "%s" "${c}/${#imgs[@]} - "
        "${rtDir}rawtherapee" -o "${outDir}${img%.*}_${v[i]}_${pp3%.*}.tif" -p "${pp3}" -t -Y -c "$img" | grep Processing
        ((c++))
    done
    ((i++))
    echo
done
printf "%s\n" "Comparing images"
hash compare 2>/dev/null || { printf "%s\n" "\"compare\" not found." "Install imagemagick (or graphicsmagick if it has \"compare\"), then re-run this script."; exit 1; }
n=1
for img in "${imgs[@]}"; do
    printf "%s\n" "${n}/${c} - ${img}"
    compare -quiet "${outDir}${img%.*}_${v[0]}_${pp3%.*}.tif" "${outDir}${img%.*}_${v[1]}_${pp3%.*}.tif" "${outDir}${img%.*}_compare.tif"
    ((n++))
done

printf "%s\n" "" "${v[0]} is ${rtDirs[0]}" "${v[1]} is ${rtDirs[1]}" "Finished"
